package com.cangshukj.web.controller.junong;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.cangshukj.common.annotation.Log;
import com.cangshukj.common.core.controller.BaseController;
import com.cangshukj.common.core.domain.AjaxResult;
import com.cangshukj.common.enums.BusinessType;
import com.cangshukj.junong.domain.SalesType;
import com.cangshukj.junong.service.ISalesTypeService;
import com.cangshukj.common.utils.poi.ExcelUtil;
import com.cangshukj.common.core.page.TableDataInfo;

/**
 * 销售服务分类Controller
 * 
 * @author wangjie
 * @date 2025-04-01
 */
@Api(value = "/sale/type",description = "销售服务分类")
@RestController
@RequestMapping("/sale/type")
public class SalesTypeController extends BaseController
{
    @Autowired
    private ISalesTypeService salesTypeService;

    /**
     * 查询销售服务分类列表
     */
    @ApiOperation("查询销售服务分类列表")
    @PreAuthorize("@ss.hasPermi('sale:type:list')")
    @GetMapping("/list")
    public AjaxResult list(SalesType salesType)
    {
        List<SalesType> list = salesTypeService.selectSalesTypeList(salesType);
        for (SalesType type:list){
            String typeId = type.getTypeId();
            List<SalesType> types = salesTypeService.selectSalesTypeByPid(typeId);
            if (types.size() > 0){
                type.setHasChildren(true);
            }else {
                type.setHasChildren(false);
            }
            type.setChildren(types);
        }

        return AjaxResult.success(list);
    }

    /**
     * 获取销售服务分类详细信息
     */
    @ApiOperation("获取销售服务分类详细信息")
    @PreAuthorize("@ss.hasPermi('sale:type:query')")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") String typeId)
    {
        return AjaxResult.success(salesTypeService.selectSalesTypeByTypeId(typeId));
    }

    /**
     * 新增销售服务分类
     */
    @ApiOperation("新增销售服务分类")
    @PreAuthorize("@ss.hasPermi('sale:type:add')")
    @Log(title = "销售服务分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalesType salesType)
    {
        salesType.setCreateBy(getUsername());
        salesType.setCreateTime(new Date());
        return toAjax(salesTypeService.insertSalesType(salesType));
    }

    /**
     * 修改销售服务分类
     */
    @ApiOperation("修改销售服务分类")
    @PreAuthorize("@ss.hasPermi('sale:type:edit')")
    @Log(title = "销售服务分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalesType salesType)
    {
        salesType.setUpdateBy(getUsername());
        salesType.setUpdateTime(new Date());
        return toAjax(salesTypeService.updateSalesType(salesType));
    }

    /**
     * 删除销售服务分类
     */
    @ApiOperation("删除销售服务分类")
    @PreAuthorize("@ss.hasPermi('sale:type:remove')")
    @Log(title = "销售服务分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable String[] typeIds)
    {
        return toAjax(salesTypeService.deleteSalesTypeByTypeIds(typeIds));
    }
}
